package copy.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
import copy.com.R;
import copy.dto.SetmealDto;
import copy.pojo.Setmeal;
import copy.pojo.SetmealDish;
import copy.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @Author 小思
 * @Description //TODO
 * @Date $ $
 * @Param $
 * @return $
 **/

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private SetmealService setmealService;

    /**
     * 手机端根据分类id 查询对应的套餐数据
     * 参数categoryId status=1
     */
    @GetMapping("/list")
    public R<List<Setmeal>> list( Setmeal setmeal) {
        //动态构造key
        String key="Setmeal_"+setmeal.getCategoryId()+ "_$";
        String s = stringRedisTemplate.opsForValue().get(key);
        if (s != null) {
            List<Setmeal> ss = JSON.parseObject(s, new TypeReference<List<Setmeal>>() {
            });
            return R.success(ss);
        }

        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Setmeal::getStatus, 1)
                .eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId, setmeal.getCategoryId())
                .orderByDesc(Setmeal::getCreateTime);
        List<Setmeal> list = setmealService.list(queryWrapper);
        stringRedisTemplate.opsForValue().set(key, JSON.toJSONString(list), 20, TimeUnit.MINUTES);
        return R.success(list);
    }
    /**
     * 保存修改的套餐数据 包含setmeal 和setmealDish
     */
    @PutMapping
    public R<String> savetwo(@RequestBody SetmealDto setmealDto) {
        setmealService.updatetwo(setmealDto);
        Set<String> keys = stringRedisTemplate.keys("Setmeal_*");
        if (keys != null) {
            stringRedisTemplate.delete(keys);
        }
        return R.success("ok");
    }

    /**
     * 套餐id查套参数据回显
     */
    /*@GetMapping("/{id}")
    public R<Setmeal> save(@PathVariable Long id) {
        Setmeal setmeal = setmealService.getById(id);
        return R.success(setmeal);
    }*/

    @GetMapping("/{id}")
    public R<SetmealDto> getById(@PathVariable Long id) {
        SetmealDto setmealDto = setmealService.qWithSeDishesWithCaBySeId(id);
        return R.success(setmealDto);
    }

    /**
     * 套餐分页查询 操作两张表 去业务层扩展写法
     * 需要对每一条套餐添加上菜品分类 的名称字段name
     * 所以扩展了数据传输对象Dto
     */
    @GetMapping("/page")
    public R<Page> page(@RequestParam int page,
                        @RequestParam int pageSize, String name) {
        Page<SetmealDto> setmealDtoPage = setmealService.pageAdd(page, pageSize, name);
        return R.success(setmealDtoPage);
    }

    /**
     * 新增套餐 两张表
     * 套餐菜品关系表中缺少setmealID
     */
    @PostMapping
    public R<String> saveWith(@RequestBody SetmealDto setmealDto) {
        Set<String> keys = stringRedisTemplate.keys("Setmeal_*");
        if (keys != null) {
            stringRedisTemplate.delete(keys);
        }
        setmealService.saveWithSetmealDish(setmealDto);
        return R.success("ok");
    }

    /**
     * 删除套餐包含菜品关系 先判断status的值 代售不能删除 ，删完套餐后删除对应的菜品数据
     * 删除套餐 判断是否关联了
     */

    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids) {
        Set<String> keys = stringRedisTemplate.keys("Setmeal_*");
        if (keys != null) {
            stringRedisTemplate.delete(keys);
        }
        setmealService.removeWithSetmealDish(ids);
        return R.success(null);
    }
    /**
     * 拷贝的批量操作
     */
    @PostMapping("/status/0")
    public R<String> update(@RequestParam List<Long> ids) {
        Set<String> keys = stringRedisTemplate.keys("Setmeal_*");
        if (keys != null) {
            stringRedisTemplate.delete(keys);
        }
        LambdaUpdateWrapper<Setmeal> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(Setmeal::getId, ids);
        updateWrapper.set(Setmeal::getStatus, 0);
        setmealService.update(updateWrapper);
        return R.success("???");
    }

    @PostMapping("/status/1")
    public R<String> updates(@RequestParam List<Long> ids) {
        Set<String> keys = stringRedisTemplate.keys("Setmeal_*");
        if (keys != null) {
            stringRedisTemplate.delete(keys);
        }
        LambdaUpdateWrapper<Setmeal> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(Setmeal::getId, ids)
                .set(Setmeal::getStatus, 1);
        setmealService.update(updateWrapper);
        return R.success("???");
    }
}
